package ncku.account.gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import ncku.account.data.GlobalData;
import ncku.account.event.Export;
import ncku.account.event.Import;
import ncku.account.event.Launcher;

public class toolBarAccount {
	//宣告元件
	JMenu  accountMenu;
	JMenuItem new_accountItem; 
	JMenuItem importItem; 
	JMenuItem exportItem;
	
	JDialog ImportDialog;
    JLabel ImportInDialog;
    JTextField name;
    JButton ImportButton;
    JButton CancelImportButton;
    //宣告變數
    private String accountName;
    
	public toolBarAccount(){
		accountMenu = new JMenu ("帳戶");
        
		new_accountItem = new JMenuItem ("新增帳戶");
		new_accountItem.addActionListener(new New_Account());
		
        importItem = new JMenuItem ("匯入帳戶");
        importItem.addActionListener(new ImportGUI());
        
        exportItem = new JMenuItem ("匯出帳戶");
        exportItem.addActionListener(new ExportGUI());
        
        accountMenu.add (new_accountItem);
        accountMenu.add (importItem);
        accountMenu.add (exportItem);
              
        ImportInDialog = new JLabel ("          請輸入要匯入之帳戶名稱：");
        ImportButton = new JButton ("確定");
        ImportButton.addActionListener(new ImportButton()); 
        CancelImportButton = new JButton ("取消");
        CancelImportButton.addActionListener(new CancelImportButton());
        name =  new JTextField (5);
	}
	
	
	class New_Account implements ActionListener{
		//建構式
		public New_Account(){
			
		}
		//動作函式
		@Override
		public void actionPerformed(ActionEvent e) {
			if(GlobalData.accountName.equals("null") == false){
				JOptionPane.showMessageDialog(Launcher.frame, "已有帳戶資料，請重啟程式以新增。", "提示訊息", JOptionPane.ERROR_MESSAGE);
			}
			else{
				//切換panel ，先把原本的移除掉，然後加入新的
				Launcher.frame.getContentPane().remove(1);
				Launcher.frame.getContentPane().add(BorderLayout.CENTER,new newAccount());	
				Launcher.frame.getContentPane().doLayout();
			}
		}
	}
	
	
	
	class ImportGUI implements ActionListener{
		//建構式
		public ImportGUI(){
			
		}
		//動作函式
		@Override
		public void actionPerformed(ActionEvent e) {
			ImportDialog = new JDialog();
			ImportDialog.setTitle("提示訊息");	
			ImportDialog.setLayout(null);
			ImportDialog.setSize(200, 180);
			ImportDialog.setLocation(600, 400);
			ImportInDialog.setBounds (0, 0, 200, 60);
			name.setBounds (40, 50, 100, 30);
			ImportButton.setBounds (30, 90, 60, 30);
			CancelImportButton.setBounds (90, 90, 60, 30);
			ImportDialog.add(ImportInDialog);
			ImportDialog.add(name);
			ImportDialog.add(ImportButton);
			ImportDialog.add(CancelImportButton);
			ImportDialog.setVisible(true);	
		}
	}
	

	class ExportGUI implements ActionListener{
		//建構式
		public ExportGUI(){
		
		}
		//動作函式
		@Override
		public void actionPerformed(ActionEvent e) {
			Export export = new Export();
			export.Start();	
			if(GlobalData.accountName.equals("null") != true){
				JOptionPane.showMessageDialog(Launcher.frame, "匯出成功囉！", "提示訊息", JOptionPane.PLAIN_MESSAGE);
			}
			else{
				JOptionPane.showMessageDialog(Launcher.frame, "匯出失敗！未有帳戶資訊。", "提示訊息", JOptionPane.ERROR_MESSAGE);
			}
			
		}
	}
	

	//匯入時輸入帳戶名稱dialog 裡面的確定鈕需做的事
		class ImportButton implements ActionListener{
			//動作函式
			@Override
			public void actionPerformed(ActionEvent e) {
				if(name.getText().length() !=0){
					//取得使用者輸入之名字
					accountName = name.getText();
					ImportDialog.dispose();
					//傳給import 函式執行
					Import Import = new Import(accountName);
					Import.Start();
					JOptionPane.showMessageDialog(Launcher.frame, "匯入成功囉！", "提示訊息", JOptionPane.PLAIN_MESSAGE);
					//切換panel ，先把原本的移除掉，然後加入新的
					Launcher.frame.getContentPane().remove(1);
					Launcher.frame.getContentPane().add (BorderLayout.CENTER,new newTransaction());
					Launcher.frame.getContentPane().doLayout();
				}
				//未輸入的錯誤處理
				else{
					JOptionPane.showMessageDialog(Launcher.frame, "匯入失敗！未輸入帳戶名稱。", "提示訊息", JOptionPane.ERROR_MESSAGE);
				}
			}
		}

		//匯入時輸入帳戶名稱dialog 裡面的取消鈕需做的事
		class CancelImportButton implements ActionListener{
			//動作函式
			@Override
			public void actionPerformed(ActionEvent e) {
				ImportDialog.dispose();
			}
		}
				
	

}
